#include"Button.h"
Button::Button()
{}
Button::Button(char *caption, int w, int h)
{
	int len;
	Caption = (char *) malloc(STR);
	strcpy(Caption, caption);
	len = strlen(Caption);
	Width = (w < len) ? len : w;
	Height = h;
}
Button::~Button()
{
	free(Caption);
}
/****************************************************************************
 *  Name       : createButton
 *  Parameters : Caption to be displayed on the Button
 *               Width of the Button (Ideally 8)
 *               Height of the Button (Ideally 1)
 *  Returns    : Nothing
 *  Purpose    : To reinitialize an existing Button object with new values.
 ****************************************************************************/
void Button::createButton(char *caption, int w, int h)
{
	int len;
	Caption = (char *) malloc(STR);
	strcpy(Caption, caption);
	len = strlen(Caption);
	Width = (w < len) ? len : w;
	Height = h;
}
/****************************************************************************
 *  Name       : setXY
 *  Parameters : X position
 *               Y position
 *  Returns    : Nothing
 *  Purpose    : Sets the coordinates of the Rectangle used to draw the
 *               Button on the screen.
 ****************************************************************************/
void Button::setXY(int x, int y)
{
	ButtonRect.left   = x;
	ButtonRect.top    = y;
	ButtonRect.right  = x + Width + 2;
	ButtonRect.bottom = y + Height;
}
/****************************************************************************
 *  Name       : setActive
 *  Parameters : Button status (active/inactive)
 *  Returns    : Nothing
 *  Purpose    : Show if Button has focus or not based on status passed.
 ****************************************************************************/
void Button::setActive(int active)
{
	if ( active )
	{
		putChar(ButtonRect.left, ButtonRect.top, BUT_L, B_HOT);
		putChar(ButtonRect.right, ButtonRect.top, BUT_R, B_HOT);
	}
	else
	{
		putChar(ButtonRect.left, ButtonRect.top, ' ', B_HOT);
		putChar(ButtonRect.right, ButtonRect.top, ' ', B_HOT);
	}
}
/****************************************************************************
 *  Name       : showButton
 *  Parameters : None
 *  Returns    : Nothing
 *  Purpose    : Draws the Button on the screen
 ****************************************************************************/
void Button::showButton()
{
	fillBox(ButtonRect, CYAN);
	putStr(ButtonRect.left + 2, ButtonRect.top, Caption, B_TXT, B_HOT);
}
/****************************************************************************
 *  Name       : handleButton
 *  Parameters : None
 *  Returns    : Character ESC, TAB or ENTER
 *  Purpose    : To handle the Button events, which are keypresses ESC, TAB
 *               and ENTER.
 ****************************************************************************/
char Button::handleButton()
{
	int key;
	setActive(1);
	while ( 1 )
	{
		if ( kbhit() )
		{
			key = getKey();
			switch ( key )
			{
				case   ESC:
				case   TAB:
				case ENTER: setActive(0);
										return key;
			} // End switch
		} // Endif (kbhit)
	} // Endwhile (Infinite)
}
// Class Button ends